package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.connect.ServerLinkControl;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.entity.servercenter.ChannelStatus;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.server.ServerProto;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.manger.system.SystemSwitchMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AgentOpServer {

	public String shutDownServer(JSONObject param) {
		JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
		int minute = param.getIntValue("minute");
		boolean shutDown = param.getBoolean("shutDown");

		ServerProto.ServerShutDownMsg.Builder shutdownMsg = ServerProto.ServerShutDownMsg.newBuilder();
		shutdownMsg.setMinute(minute);
		shutdownMsg.setShutdown(shutDown);

		YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.A_SERVER_SHUTDOWN, shutdownMsg);
		ServerLinkControl.send(0, pkg);

		AgentInterfaceMgr.setCommonRet(resultObj, eAgentErrorCode.Success.getValue());
		return resultObj.toString();
	}

	public String getServerSystemSwitchList(JSONObject param) {
		JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
		List<Integer> lockSystemIdList = SystemSwitchMgr.getLockSystemIdList();
		AgentInterfaceMgr.setData(resultObj, StringUtils.listToString(lockSystemIdList, ","));
		AgentInterfaceMgr.setCommonRet(resultObj, eAgentErrorCode.Success.getValue());
		return resultObj.toString();
	}

	public String getServerSystemChannelStatusList(JSONObject param) {
		JSONObject ret = AgentInterfaceMgr.getDefaultRet();
		Map<Integer, ChannelStatus> channelStatusMap = SystemSwitchMgr.getChannelStatusMap();
		ArrayList<ChannelStatus> channelStatusArrayList = new ArrayList<>();
		channelStatusMap.forEach((k, v) -> {
			channelStatusArrayList.add(v);
		});
		AgentInterfaceMgr.setData(ret, StringUtils.listToString(channelStatusArrayList, ","));
		AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
		return ret.toString();
	}

}
